import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description
 * User: LianBao
 * Date: 2024-04-24
 * Time: 18:35
 */
class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}

public class Test {
    public List<String> topKFrequent(String[] words, int k) {
        List<String> ret = new ArrayList<>();
        Map<String, Integer> map = new HashMap<>();
        //统计单词次数
        for (int i = 0; i < words.length; i++) {
            if (!map.containsKey(words[i])) {
                map.put(words[i], 1);
            } else {
                map.put(words[i], map.get(words[i]) + 1);
            }
        }

        PriorityQueue<Map.Entry<String, Integer>> queue = new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if (o1.getValue().compareTo(o2.getValue()) == 0) {
                    return o2.getKey().compareTo(o1.getKey());
                }
                return o1.getValue().compareTo(o2.getValue());
            }
        });//建小根堆

        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (queue.size() < k) {
                queue.offer(entry);
            } else {
                Map.Entry<String, Integer> top = queue.peek();
                if (top.getValue().compareTo(entry.getValue()) == 0) {
                    //次数相同
                    if (top.getKey().compareTo(entry.getKey()) > 0) {
                        queue.poll();
                        queue.offer(entry);
                    }
                } else {
                    //次数不同
                    if (top.getValue().compareTo(entry.getValue()) < 0) {
                        queue.poll();
                        queue.offer(entry);
                    }
                }
            }
        }
        for (int i = 0; i < k; i++) {
            ret.add(queue.poll().getKey());
        }
        Collections.reverse(ret);
        return ret;
    }

    public static void main(String[] args) {
        int[] a = {1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 7, 7};

        //去重
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < a.length; i++) {
            if (!set.contains(a[i])) {
                set.add(a[i]);
            }
        }
        System.out.println(set);


        //统计重复的数据 出现的次数
        char[] chars = {'a', 'a', 'c', 'd'};
        Map<Character, Integer> CharsMap = new HashMap<>();
        for (int i = 0; i < chars.length; i++) {
            if (!CharsMap.containsKey(chars[i])) {
                CharsMap.put(chars[i], 1);
            } else {
                CharsMap.put(chars[i], CharsMap.get(chars[i]) + 1);
            }
        }

        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < a.length; i++) {
            if (!map.containsKey(a[i])) {
                map.put(a[i], 1);
            } else {
                map.put(a[i], map.get(a[i]) + 1);
            }
        }
        System.out.println(Arrays.toString(a));
        System.out.println(map);
        System.out.println("============");
        System.out.println(CharsMap);
    }


    public Node copyRandomList(Node head) {
        HashMap<Node, Node> map = new HashMap<>();
        Node cur = head;
        while (cur != null) {
            Node node = new Node(cur.val);
            map.put(cur, node);
            cur = cur.next;
        }

        cur = head;

        while (cur != null) {
            //map.get(cur).next
            map.get(cur).next = map.get(cur.next);
            //map.get(cur).random
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }

        return map.get(head);
    }

    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String s1 = in.nextLine();//应该输入的
            String s2 = in.nextLine();//实际输入的
            fun(s1, s2);
        }
    }

    public static void fun(String str1, String str2) {
        HashSet<Character> set = new HashSet<>();
        char[] s1 = str1.toUpperCase().toCharArray();
        char[] s2 = str2.toUpperCase().toCharArray();
        for (int i = 0; i < s2.length; i++) {
            set.add(s2[i]);//实际输入的字符
        }
        HashSet<Character> ret = new HashSet<>();

        for (int i = 0; i < s1.length; i++) {
            if (!set.contains(s1[i]) && !ret.contains(s1[i])) {
                ret.add(s1[i]);
                System.out.print(s1[i]);
            }
        }
    }
}
